package net.imagine.provider.skyline;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import net.imagine.core.resize.ImageResizeTask;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 * 本地图片压缩实现
 * 
 * @author wuqh
 * @see ImageResizeTask
 */
public class LocalImageResizeTask extends SkylineImageResizeTask {
	private final String localStorePath;
	private final int[] baseSizes;
	private final File imageFile;

	/***************************************************************************
	 * @param localStorePath
	 *            图片的本地存放目录
	 * @param baseSizes
	 *            要压缩的最大长/宽值数列
	 **************************************************************************/
	public LocalImageResizeTask(String localStorePath, int[] baseSizes) {
		this.baseSizes = baseSizes;
		this.localStorePath = localStorePath;
		this.imageFile = new File(localStorePath);
	}

	@Override
	public int[] getBaseSizes() {
		return this.baseSizes;
	}

	@Override
	public byte[] getData() throws IOException {
		return FileUtils.readFileToByteArray(imageFile);
	}

	@Override
	public long getFileSize() {
		return imageFile.length();
	}

	@Override
	public String getFilename() {
		return FilenameUtils.getName(localStorePath);
	}

	@Override
	public InputStream getInputStream() throws IOException {
		return FileUtils.openInputStream(imageFile);
	}

	@Override
	public String getLocalStorePath() {
		return this.localStorePath;
	}

	@Override
	public void setLocalStorePath(String localStorePath) {
		throw new UnsupportedOperationException("cannot set localStorePath in class LocalImageResizeTask");
	}

	@Override
	public String getRemoteFileKey() {
		return null;
	}

}
